package one.chapter_1.unionfind;

public class QuickUnionUF extends AbstractUF {
    private int []ids;
    public QuickUnionUF(int count) {
        super(count);
        ids=new int[count];
    }

    @Override
    public void union(int p, int q) {
        int rootP=find(p);
        int rootQ=find(q);
        if (rootP == rootQ){
            return;
        }



        ids[rootQ]=rootP;
        count--;
    }

    @Override
    public int find(int p) {
        while (ids[p]!=p){
            p=ids[p];
        }
        return p;
    }

    @Override
    public boolean connected(int p, int q) {

        return find(p)==find(q);

    }
}
